from time import sleep

import ddt as ddt
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from ConfigFile.Varconfig import chromePath, testData_path
from Modules.loginMoudles import LoginAction
from Util.ExcelOperate import ExcelOperate
import unittest
from Util.Logger import Log

# from Util.Logger import Log
from Util.util import assert_until, get_testdata


# def test_login():
#     try:
#         # chrome_Path = 'E:\RF\chromedriver.exe'
#         # options = Options()
#         # options.add_argument('--start-maximized')
#         # wd = webdriver.Chrome(executable_path=chromePath, options=options)
#         # wd.get('https://mail.163.com/')
#         #
#         # loginActions = LoginAction()
#         # loginActions.login(wd,'13037521553','Ruru970929.')
#
#         excel = ExcelOperate()
#         excel.load_workbook(testData_path)
#         excel.get_sheet_by_name('Sheet1')
#         row_nums = excel.get_rows_nums()
#         row_1_values = excel.get_row_values(1)
#         # print(row_1_values)
#
#         for i in range(2,row_nums+1):
#             row_values = excel.get_row_values(i)
#             # 组装字典  zip先组装成元组类型一一对应  dict进行字典转换
#             values = dict(zip(row_1_values,row_values))
#             print(values)
#             # 判断该用例是否执行
#             if values['is_executed'].lower() == 'y':
#                 username = values['username']
#                 password = values['password']
#                 options = Options()
#                 options.add_argument('--start-maximized')
#                 wd = webdriver.Chrome(executable_path=chromePath, options=options)
#                 wd.get('https://mail.163.com/')
#                 loginActions = LoginAction()
#                 loginActions.login(wd,username,password)
#                 sleep(2)
#                 assert_type,assert_value = values['except_result'].split(':')
#                 try:
#                     # 不用unittest 进行断言 并写入测试结果
#                     assert_until(wd,assert_type,assert_value)
#                 except AssertionError:
#                     excel.write_cell(i,7,'fail')
#                 else:
#                     excel.write_cell(i,7,'pass')
#                 wd.quit()
#
#     except Exception as e:
#         print(e)

#  unittest 用法
@ddt.ddt
class Login_Test(unittest.TestCase):

    @classmethod
    def setUp(cls):
            print('='*10 + 'set up' + '='*10)
            option = Options()
            option.add_argument('--start-maximized')
            cls.wd = webdriver.Chrome(chromePath,options=option)
            cls.wd.get('http://mail.163.com')
    @classmethod
    def tearDown(cls):
            print('=' * 10 + 'teardown' + '=' * 10)
            sleep(3)
            cls.wd.quit()

    '''每个用例都会执行setup teardown  故改造'''
    # @classmethod
    # # def setUpclass(cls):
    # #     print('='*10 + 'set up' + '='*10)

    # @classmethod
    # def tearDownclass(cls):
    #     print('=' * 10 + 'teardown' + '=' * 10)

    test_data = get_testdata()
    @ddt.data(*test_data) #[[xx],[xxx]]
    @ddt.unpack   # 不加unpack 会将整个大列表传入一次  加上可以将里面的列表都传入一次
    def test_01(self,id,desc,username,pwd,is_executed,except_result,test_result):
        excel = ExcelOperate()
        excel.load_workbook(testData_path)
        excel.get_sheet_by_name('Sheet1')
        if is_executed.lower() == 'y':
            logger = Log()
            logger.info(f'run {desc}')
            loginAction = LoginAction()
            loginAction.login(wd,username,pwd)
            sleep(3)
            assert_type,assert_value = except_result.split(':')
            try:
                assert_until(self.wd,assert_type,assert_value)
            except AssertionError as e:
                # Log.error(e.message)
                excel.write_cell(2,7,'fail')
            else:
                excel.write_cell(2,7,'pass')

    # def test_02(self):
    #     print('test02')
    #     self.assertEqual(1,1)
    #
    # def test_03(self):
    #     print('test03')
    #     self.assertEqual(2,2)



if __name__ == '__main__':
    # test_login()
    unittest.main()
'''
testcase test开头  且执行顺序会按照方法名的ASCII值去排序
test fixure 测试固件（夹具）  
test suit   测试套件 需要执行的用例集中起来   用testload来加载测试用例到测试套件中
test runner 用来执行测试用例，并返回测试结果   HTMLTestrunner

常见断言
assertEqual(a,b,msg=No) | a==b       assertNotEaual
assertTrue(a)           | a is True  assertFlase
assertIs(a,b)           | a is b     assertIsNot
assertIsNone(a)         | a is None  assertIsNotNone
assertIn(a,b)           | a in b     assertNotIn
assertIsInstance(a,b)   | isinstance(a,b)  assertNotInstance


----> AssertionError 断言失败

'''